package service;

import common.Pagination;
import dao.CroBillsDao;
import entity.CroBills;
import entity.CroCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin on 2017/5/21.
 */
@Service
public class CroBillsService {
    @Autowired
    private CroBillsDao croBillsDao;

    /**
     * 添加消费记录
     * @param bills
     * @return
     */
    @Transactional(readOnly = false)
    public boolean addBills(CroBills bills){
        try {
            croBillsDao.save(bills);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 列出所有消费记录
     * @return
     */
    @Transactional(readOnly = true)
    public List<CroBills> listByCus(CroCustomer customer){
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("del_flag","0");
        return croBillsDao.findByProperty(maps);
    }

    /**
     * 列出所有消费记录(分页)
     * @return
     */
    @Transactional(readOnly = true)
    public Pagination<CroBills> listPageByCus(CroCustomer customer, int index, int size){
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("croCustomer",customer);
        maps.put("del_flag","0");
        return croBillsDao.findPaginationByPropertyInvert(maps,"costDate",index,size);
    }

    /**
     * 通过Id查找记录
     * @param Id
     * @return
     */
    @Transactional(readOnly = true)
    public CroBills findById(String Id){
        return croBillsDao.getById(Id);
    }

    /**
     * 更新消费记录
     * @param bills
     * @return
     */
    @Transactional(readOnly = false)
    public boolean updateBills(CroBills bills) {
        try {
            croBillsDao.update(bills);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
